package com.dimeng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dimeng.utils.AppCache;
import com.dimeng.utils.Constant;
import com.dimeng.utils.LoadDataThread;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author write by ccn<br>
 * <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-6-26 下午11:38:32<br>
 * descrition:用于过滤搜索结果所属地区的视图
 */
public class FilterRegionActivity extends SuperActivity {
	private static final long serialVersionUID = 1270423302903601383L;
	private TextView provinceLabelTextView;
	private TextView cityLabelTextView;
	private LinearLayout bgLineLayout;
	private ListView listView;
	private SimpleAdapter adapter;
	private Intent intent;
	private boolean filterProvincesFlag = true;
	private List<HashMap<String,String>> filterRegions = new ArrayList<HashMap<String,String>>();
	private String loadRegionsUrl;
	private final String filterProvDo = Constant.HOST_URL +"filterProvinces.do";
	private final String filterCityDo = Constant.HOST_URL +"filterCities.do";
	private Thread loadRegionsThread;
	private Resources resources;
	private String keyword = "";
	private int catId;
	private int searchType;
	private String catName;
	private Class<Activity> className;
	private static HashMap<String,String> allRegionMap = new HashMap<String, String>();
	static {
		allRegionMap.put("regionId", "");
		allRegionMap.put("regionName", "所有地区    >");
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
				case 1:
					String json = (String) msg.obj;
					List<HashMap<String,String>> loadDatas = new Gson().fromJson(json, new TypeToken<List<HashMap<String,String>>>(){}.getType());
					filterRegions.clear();
					if(filterProvincesFlag){
						AppCache.filterProvinces.clear();
						AppCache.filterProvinces.add(allRegionMap);
						AppCache.filterProvinces.addAll(loadDatas);
						filterRegions.addAll(AppCache.filterProvinces);
					} else {
						AppCache.filterCities.clear();
						AppCache.filterCities.add(allRegionMap);
						AppCache.filterCities.addAll(loadDatas);
						filterRegions.addAll(AppCache.filterCities);
					}
					listView.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					break;
				case 2:
					Toast.makeText(FilterRegionActivity.this, Constant.NET_ERROR, Toast.LENGTH_LONG).show();
					break;
				default:
					break;
			}
		}
	};
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_region);
		intent = getIntent();// TODO get search params
		resources = getResources();
		searchType = intent.getIntExtra("searchType", 0);
		keyword = intent.getStringExtra("keyword");
		className = (Class<Activity>) intent.getSerializableExtra("class");
		catId = intent.getIntExtra("catId",0);
		catName = intent.getStringExtra("catName");
		
		provinceLabelTextView = (TextView) findViewById(R.id.provinceLabelTextViewId);
		cityLabelTextView = (TextView) findViewById(R.id.cityLabelTextViewId);
		bgLineLayout = (LinearLayout) findViewById(R.id.bgLineLayoutId);
		
		loadRegionsUrl = filterProvDo;
		filterRegions.addAll(AppCache.filterProvinces);
		listView = (ListView) findViewById(R.id.regionListViewId);
		adapter = new SimpleAdapter(this, filterRegions, 
				R.layout.filter_region_item, new String[]{"regionId","regionName"}, 
				new int[]{R.id.filterRegionId,R.id.filterRegionNameId});
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		if(0 == filterRegions.size()){
			startLoadRegionsThread();
		}
		
		provinceLabelTextView.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				filterProvincesFlag = true;
				provinceLabelTextView.setTextColor(Color.rgb(255, 0, 0));
				cityLabelTextView.setTextColor(Color.rgb(0, 0, 0));
				Drawable db = resources.getDrawable(R.drawable.bgline01);
				bgLineLayout.setBackgroundDrawable(db);
				
				filterRegions.clear();
				filterRegions.addAll(AppCache.filterProvinces);
				loadRegionsUrl = filterProvDo;
				if(0 == filterRegions.size()){
					startLoadRegionsThread();
				}
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}}
		);
		cityLabelTextView.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				filterProvincesFlag = false;
				cityLabelTextView.setTextColor(Color.rgb(255, 0, 0));
				provinceLabelTextView.setTextColor(Color.rgb(0, 0, 0));
				Drawable db = resources.getDrawable(R.drawable.bgline02);
				bgLineLayout.setBackgroundDrawable(db);
				filterRegions.clear();
				filterRegions.addAll(AppCache.filterCities);
				loadRegionsUrl = filterCityDo;
				if(0 == filterRegions.size()){
					startLoadRegionsThread();
				}
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}}
		);
		
		listView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				HashMap<String,String> selectedItemMap = (HashMap<String, String>) listView.getItemAtPosition(position);
				String regionId = selectedItemMap.get("regionId");
				String regionName = selectedItemMap.get("regionName");
				Intent intent = new Intent(FilterRegionActivity.this,className);
				intent.putExtra("regionId", regionId);
				intent.putExtra("regionName", regionName);
				intent.putExtra("searchType", searchType);
				intent.putExtra("keyword", keyword);
				intent.putExtra("catId", catId);
				intent.putExtra("catName", catName);
				System.out.println("FilterRegionActivity regionId="+regionId+" regionName="+regionName+" keyword="+keyword+" catId="+catId + " "+className);
				FilterRegionActivity.this.startActivity(intent);
			}}
		);
	}
	
	private void startLoadRegionsThread(){
		if(null == loadRegionsThread || !loadRegionsThread.isAlive()){
			loadRegionsThread = new LoadDataThread(loadRegionsUrl,Constant.CHARSET_GBK,handler,true);
			loadRegionsThread.start();
		}
	}
	
}
